﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Movies.CopyUtility;

namespace Movies
{
    public partial class FormMigrateData : Form
    {
        public FormMigrateData()
        {
            InitializeComponent();
        }

        private void moveSetting(String src, String dst)
        {
            try
            {
                if (!Properties.Settings.Default.dataStoreLocation.Equals(""))
                {
                    HelperFileDetailsGuide guide = new HelperFileDetailsGuide(src);
                    DTOFileMovementReport report = guide.setDestination(dst);
                    if (!report.IsDestinationWriteAllowed)
                    {
                        WinDisplayControllor.DisplayDialog("Destination is not available for moving Movie(s)");
                        return;
                    }

                    if (!report.IsSizeAvailbale)
                    {
                        WinDisplayControllor.DisplayDialog("Destination doesn't have sufficient available space");
                        return;
                    }
                    guide.startMove(null);

                }
            }
            catch (Exception exe)
            {
                HelperContentProvider.OmitWarning(exe);
            }
        }

        private void FormMigrateData_Load(object sender, EventArgs e)
        {
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;
            timer.Dispose();
            migrateProgress.Maximum = 10;
            migrateProgress.Value = 0;
            if (Properties.Settings.Default.dataStoreLocation.Equals(""))
            {
                string appPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                string appPathOld = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                //Environment.SpecialFolder.MyDocuments;
                Properties.Settings.Default.oldDataStoreLocation = appPathOld + "\\MyMo_DataStore";
                Properties.Settings.Default.dataStoreLocation = appPath + "\\MyMoMan";

                Properties.Settings.Default.movieDataStoreLocation = Properties.Settings.Default.dataStoreLocation + "\\MovieData";
                Properties.Settings.Default.Save();
                migrateProgress.Value = 2;
            }
            else if (!Properties.Settings.Default.dataStoreLocation.Equals("") && Properties.Settings.Default.oldDataStoreLocation.Equals(""))
            {
                Properties.Settings.Default.oldDataStoreLocation = Properties.Settings.Default.dataStoreLocation;
                string appPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
                Properties.Settings.Default.dataStoreLocation = appPath + "\\MyMoMan";
                Properties.Settings.Default.movieDataStoreLocation = Properties.Settings.Default.dataStoreLocation + "\\MovieData";
                Properties.Settings.Default.Save();
                migrateProgress.Value = 3;
            }

            if (Directory.Exists(Properties.Settings.Default.oldDataStoreLocation))
            {
                if (!Directory.Exists(Properties.Settings.Default.dataStoreLocation))
                {
                    Directory.CreateDirectory(Properties.Settings.Default.dataStoreLocation);
                    migrateProgress.Value = 4;
                }

                if (Directory.Exists(Properties.Settings.Default.oldDataStoreLocation))
                {

                    try
                    {
                        Directory.Move(Properties.Settings.Default.oldDataStoreLocation, Properties.Settings.Default.movieDataStoreLocation + "\\MovieData");
                        //Directory.Move(Properties.Settings.Default.dataStoreLocation + "\\MyMo_DataStore", Properties.Settings.Default.dataStoreLocation + "\\MovieData");
                    }
                    catch (Exception ex)
                    {

                    }
                    migrateProgress.Value = 6;
                }
                if (Directory.Exists(Properties.Settings.Default.movieDataStoreLocation + "\\UserData"))
                {
                    Directory.Move(Properties.Settings.Default.movieDataStoreLocation + "\\UserData", Properties.Settings.Default.dataStoreLocation);
                    migrateProgress.Value = 7;
                }

            }

            HelperSettingProvider.SettingLocation(Properties.Settings.Default.dataStoreLocation + "\\AppData");
            if (!Directory.Exists(Properties.Settings.Default.dataStoreLocation + "\\AppData"))
            {
                Directory.CreateDirectory(Properties.Settings.Default.dataStoreLocation + "\\AppData");
                HelperSettingProvider.Default().ButtonSize = "" + Properties.Settings.Default.buttonSize;
                HelperSettingProvider.Default().DataStoreLocation = "" + Properties.Settings.Default.dataStoreLocation;
                HelperSettingProvider.Default().DefaultDisplay = "" + Properties.Settings.Default.defaultDisplay;
                HelperSettingProvider.Default().DefaultDisplay = "" + Properties.Settings.Default.defaultStore;
                HelperSettingProvider.Default().ExtList = "" + Properties.Settings.Default.ExtList;
                HelperSettingProvider.Default().FileActionLastSelectedPath = "" + Properties.Settings.Default.FileActionLastSelectedPath;
                HelperSettingProvider.Default().FirstName = "" + Properties.Settings.Default.firstName;
                HelperSettingProvider.Default().FolderNames = "" + Properties.Settings.Default.folderNames;
                HelperSettingProvider.Default().IsSynchronized = "" + Properties.Settings.Default.IsSynchronized;
                HelperSettingProvider.Default().LastName = "" + Properties.Settings.Default.lastName;
                HelperSettingProvider.Default().MovieDataStoreLocation = "" + Properties.Settings.Default.movieDataStoreLocation;
                HelperSettingProvider.Default().NameChangeInterest = "" + Properties.Settings.Default.NameChangeInterest;
                HelperSettingProvider.Default().NickName = "" + Properties.Settings.Default.nickName;
                HelperSettingProvider.Default().OldDataStoreLocation = Properties.Settings.Default.oldDataStoreLocation;
                HelperSettingProvider.Default().QuickStartShown = "" + Properties.Settings.Default.quickStartShown;
                HelperSettingProvider.Default().TmdbAPIKey = "" + Properties.Settings.Default.tmdbAPIKey;
                migrateProgress.Value = 8;
                HelperSettingProvider.Default().Save();
                migrateProgress.Value = 10;
            }

            Properties.Settings.Default.migrationDone = true;
            Properties.Settings.Default.Save();

            this.Dispose();
        }
    }
}
